package aidn.animation.transition 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class Mosaic 
	{
		private var _target :MovieClip;
		private var _bitmap :Bitmap;
		
		private var _blankBmd :BitmapData;
		
		private var _tmp :BitmapData;
		private var _bmd :BitmapData;
		
		private var _matrix :Matrix = new Matrix();
		
		private var _n     :int;
		private var _speed :int;
		
		public function Mosaic ( ) 
		{
			
		}
		
		public function init ( target :MovieClip ) :void
		{
			_target = target;
			_target.addEventListener(Event.ENTER_FRAME, _enterFrame);
			
			_n     = 100;
			_speed = 2;
			
			
			if (_blankBmd)
			{
				_blankBmd.dispose();
				_blankBmd = null;
			}
			_blankBmd = new BitmapData(_target.width, _target.height, true, 0x0);
			
			_tmp = new BitmapData(_target.width, _target.height, true, 0x0);
			_bmd = new BitmapData(_target.width, _target.height, true, 0x0);
			
			_bitmap = new Bitmap(_bmd);
			
			_bitmap.x = _target.x;
			_bitmap.y = _target.y;
			_target.parent.addChild(_bitmap);
		}
		
		private function _enterFrame ( evt :Event ) :void 
		{
			_tmp.copyPixels(_blankBmd, _blankBmd.rect, new Point());
			_bmd.copyPixels(_blankBmd, _blankBmd.rect, new Point());
			
			_matrix.identity();
			_matrix.scale(1 / _n, 1 / _n);
			
			_tmp.draw(_target, _matrix);
			
			_matrix.identity();
			_matrix.scale(_n, _n);
			
			_bmd.draw(_tmp, _matrix);
			
			// _n == 1
			
			_n -= _speed;
			if (_n < 1) _n = 1;
		}		
	}
}